package com.wn.storage.web;

import com.wn.storage.entity.Storage;
import com.wn.storage.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("storage")
@Slf4j
public class StorageController {

    @Autowired
    private StorageService storageService;

    @GetMapping("{id}")
    public Storage getStorageById(@PathVariable("id") Integer id) {
        return storageService.findById(id);
    }

    /**
     * 扣减库存
     * @param code  商品编号
     * @param count 要扣减的数量
     * @return 无
     */
    @PutMapping("/{code}/{count}")
    public ResponseEntity<Void> deduct(
            @PathVariable("code") String code, @PathVariable("count") Integer count) {

        log.info("请求参数:{},{}", code, count);
        storageService.deduct(code, count);
        return ResponseEntity.noContent().build();
    }
}
